from rest_framework import serializers

from goods.models import GoodsCategory, GoodsVisitCount
from users.models import User

# class UserSerializer(serializers.ModelSerializer):
#
#     class Meta:
#         model=User
#         fields=('id','username','mobile','email')

# class UserSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = User
#         fields = ('id', 'username', 'mobile', 'email', 'password')
#         # username字段增加长度限制，password字段只参与保存，不在返回给前端，增加write_only选项参数
#         extra_kwargs = {
#             'username': {
#                 'max_length': 20,
#                 'min_length': 5
#             },
#             'password': {
#                 'max_length': 20,
#                 'min_length': 8,
#                 'write_only': True
#
#             },
#         }
#     # 重写create方法
#     def create(self, validated_data):
#         # 保存用户数据并对密码加密
#         user = User.objects.create_user(**validated_data)
#         return user
class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ('id','username','mobile','email','password')
        extra_kwargs = {'password':{'write_only':True},}

    def create(self,validated_data):
        user = User.objects.create_user(**validated_data)
        return user


class GoodsDaySerializer(serializers.ModelSerializer):

    category = serializers.StringRelatedField(read_only=True)

    class Meta:
        model = GoodsVisitCount
        fields = '__all__'
